/*
 * Copyright(c) 2018 优居科技 All rights reserved.
 * distributed with this file and available online at
 * http://www.yjyz.com/
 */
package com.zb.api.business.service.impl;


import com.zb.api.business.dto.PosPlanDto;
import com.zb.api.business.dto.PosUserCollectDto;
import com.zb.api.business.entity.PosUserCollectEntity;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.mapper.PosPlanMapper;
import com.zb.api.business.mapper.PosUserCollectMapper;
import com.zb.api.business.service.IPosUserCollectService;
import com.zb.api.common.econts.DecorationStyleEnum;
import com.zb.api.common.models.Page;
import com.zb.api.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
* 用户收藏表
 *
 * @author cwp
 * @version v1.0
 * @CreationTime: - 2022-02-08 14:19:31
 * @Description:
 */
@Service
public class PosUserCollectServiceImpl implements IPosUserCollectService {

    @Autowired
    private PosUserCollectMapper posUserCollectMapper;

    @Autowired
    private PosPlanMapper posPlanMapper;

    @Override
    public Page<PosUserCollectDto> getPosUserCollectList(int pageNo, int pageSize, PosUserCollectDto posUserCollectDto){
        /*PageHelper.startPage(pageNo, pageSize);
        PosUserCollectEntity posUserCollectEntity = BeansUtils.copyProperties(posUserCollectDto, PosUserCollectEntity.class);
        return BeansUtils.toPage(posUserCollectMapper.getList(posUserCollectEntity), PosUserCollectDto.class);*/
        return null;
    }

    @Override
    public PosUserCollectDto get(String id) {
        PosUserCollectDto collectDto = new PosUserCollectDto();
        BeanUtils.copyProperties(posUserCollectMapper.get(id), collectDto);
        return collectDto;
    }

    @Override
    public int update(PosUserCollectDto posUserCollect){
        PosUserCollectEntity entity = new PosUserCollectEntity();
        BeanUtils.copyProperties(posUserCollect, entity);
        return posUserCollectMapper.update(entity);
    }

    @Override
    public int insert(PosUserCollectDto posUserCollect){
        UserEntity user =  UserThreadLocal.get();

        PosUserCollectEntity entity = new PosUserCollectEntity();
        BeanUtils.copyProperties(posUserCollect, entity);
        entity.setCreateTm(new Date().getTime());
        entity.setUserId(user.getId());
        return posUserCollectMapper.insert(entity);
    }

    @Override
    public int delete(Integer id){
        PosUserCollectEntity entity = new PosUserCollectEntity();
        entity.setId(id);
        return posUserCollectMapper.delete(entity);
    }

    @Override
    public Page<PosPlanDto> getUserCollectList(PosUserCollectDto posUserCollectDto) {
        UserEntity user =  UserThreadLocal.get();
        Page<PosPlanDto> page = new Page<>();
        page.setPageNo(posUserCollectDto.getPageNo());
        page.setRowCntPerPage(posUserCollectDto.getPageSize());
        posUserCollectDto.setUserId(user.getId());
        List<PosPlanDto> list = posUserCollectMapper.getUserCollectList(posUserCollectDto);
        if (!CollectionUtils.isEmpty(list)){
            for (PosPlanDto posPlanDto : list) {
                if (posPlanDto.getThemeStyle()!=null){
                    posPlanDto.setThemeStyleName(DecorationStyleEnum.value(posPlanDto.getThemeStyle()));
                }
            }
        }
        page.setList(list);
        return page;
    }
}
